<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
defined('_JEXEC') or die;

class CprojectHelper {

    public static function addSubmenu($submenu) {
        JSubMenuHelper::addEntry(JText::_('Cproject'), 'index.php?option=com_cproject', $submenu == 'messages');
        JSubMenuHelper::addEntry(JText::_('Cproject Categories'), 'index.php?option=com_cproject&view=categories', $submenu == 'categories');
        JSubMenuHelper::addEntry(JText::_('Cproject Setions'), 'index.php?option=com_cproject&view=sections', $submenu == 'sections');
        // set some global property
//        $document = JFactory::getDocument();
//        $document->addStyleDeclaration('.icon-48-helloworld {background-image: url(../media/com_helloworld/images/tux-48x48.png);}');
//        if ($submenu == 'categories') {
//            $document->setTitle(JText::_('COM_BOOKS_ADMINISTRATION_CATEGORIES'));
//        }
    }

    public function getActions() {
        $user = JFactory::getUser();
        $result = new JObject;
        $assetName = 'com_cproject';

        $actions = array(
            'core.admin', 'core.manage', 'core.create', 'core.edit', 'core.edit.own', 'core.edit.state', 'core.delete'
        );

        foreach ($actions as $action) {
            $result->set($action, $user->authorise($action, $assetName));
        }

        return $result;
    }

    function getDays($typedate =null) {

        $days = array();
        for ($i = 1; $i <= 31; $i++) {

            $days[] = $i;
        }
        $html = array();
       
        if ($days) {
            for ($x = 1; $x < count($days); $x++) {
                $html = JHtml::_('select.genericlist', $days, $typedate, '', 'id', 'name', $days[$x]);
            }
        }
        return $html;
    }
    function getMonths($typedate=null) {

        $months = array();
        for ($i = 1; $i <= 12; $i++) {

            $months[] = $i;
        }
        $html = array();
       
        if ($months) {
            for ($x = 1; $x < count($months); $x++) {
                $html = JHtml::_('select.genericlist', $months, $typedate, '', 'id', 'name', $months[$x]);
            }
        }
        return $html;
    }
     function getYears() {

        $years = array();
        $yearnow = gmdate('Y');
        for ($i = 1990; $i <= $yearnow; $i++) {

            $years[] = $i;
        }
        $html = array();
       
        if ($years) {
            for ($x = 1; $x < count($years); $x++) {
                $html = JHtml::_('select.genericlist', $years, 'year', '', 'id', 'name', $years[$x]);
            }
        }
        return $html;
    }

    public static function alias($text) {
        $arr_u = array('ú', 'ủ', 'ụ', 'ũ', 'ù', 'ư', 'ứ', 'ử', 'ự', 'ữ', 'ừ', 'Ú', 'Ủ', 'Ụ', 'Ũ', 'Ù', 'Ư', 'Ứ', 'Ử', 'Ự', 'Ữ', 'Ừ');
        $arr_e = array('é', 'ẻ', 'ẹ', 'ẽ', 'è', 'ê', 'ế', 'ể', 'ệ', 'ễ', 'ề', 'É', 'Ẻ', 'Ẹ', 'Ẽ', 'È', 'Ê', 'Ế', 'Ể', 'Ệ', 'Ễ', 'Ề');
        $arr_i = array('í', 'ỉ', 'ị', 'ĩ', 'ì', 'Í', 'Ỉ', 'Ị', 'Ĩ', 'Ì');
        $arr_y = array('ý', 'ỷ', 'ỵ', 'ỹ', 'ỳ', 'Ý', 'Ỷ', 'Ỵ', 'Ỹ', 'Ỳ');
        $arr_a = array('á', 'ả', 'ạ', 'ã', 'à', 'â', 'ấ', 'ẩ', 'ậ', 'ẫ', 'ầ', 'ă', 'ắ', 'ẳ', 'ặ', 'ẵ', 'ằ', 'Á', 'Ả', 'Ạ', 'Ã', 'À', 'Â', 'Ấ', 'Ẩ', 'Ậ', 'Ẫ', 'Ầ', 'Ă', 'Ắ', 'Ẳ', 'Ặ', 'Ẵ', 'Ằ');
        $arr_o = array('ó', 'ỏ', 'ọ', 'õ', 'ò', 'ô', 'ố', 'ổ', 'ộ', 'ỗ', 'ồ', 'ơ', 'ớ', 'ở', 'ợ', 'ỡ', 'ờ', 'Ó', 'Ỏ', 'Ọ', 'Õ', 'Ò', 'Ô', 'Ố', 'Ổ', 'Ộ', 'Ỗ', 'Ồ', 'Ơ', 'Ớ', 'Ở', 'Ợ', 'Ỡ', 'Ờ');
        $arr_d = array('Đ', 'đ');
        $arr_ = array('_', ' ');

        //$text = strtolower($text);
        $text = str_replace($arr_u, 'u', $text);
        $text = str_replace($arr_e, 'e', $text);
        $text = str_replace($arr_i, 'i', $text);
        $text = str_replace($arr_y, 'y', $text);
        $text = str_replace($arr_a, 'a', $text);
        $text = str_replace($arr_o, 'o', $text);
        $text = str_replace($arr_d, 'd', $text);
        $text = str_replace($arr_, '-', $text);

        return strtolower($text);
    }

}

class CprojectDBHelper {

    function getCprojectSection($sql, $where = array(), $order = array(), $limit = 0, $group = array()) {
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select("a.*");
        $query->select("CONCAT(a.id,':',a.alias) AS gslug");
        $query->from('#__cproject_sections AS a');

        // Join over the user
        $query->select('uc.name AS created_by');
        $query->join('LEFT', '`#__users` AS uc ON uc.id = a.created_by');


        // Join over the user
        $query->select('um.name AS modified_by');
        $query->join('LEFT', '`#__users` AS um ON um.id = a.modified_by');


        // add "where" into sql statement
        if (count($where)) {
            foreach ($where as $s) {
                $query->where($s);
            }
        }

        // add "order" into sql statement
        if (count($order)) {
            foreach ($order as $s) {
                $query->order($s);
            }
        } else {
            $query->order('a.id');
        }

        // add "group" into sql statement
        if (count($group)) {
            foreach ($group as $s) {
                $query->group($s);
            }
        }

        if ($sql) {
            return $query;
        } else {
            if ($limit > 0) {
                $db->setQuery($query, $limit);
            } else {
                $db->setQuery($query);
            }
            return $db->loadObjectList();
        }
    }

    function getCprojectCategories($sql, $where = array(), $order = array(), $limit = 0, $group = array()) {

        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select("a.*");
        $query->select("CONCAT(a.id,':',a.alias) AS gslug");
        $query->from('#__cproject_categories AS a');

        // Join over the user
        $query->select('ss.name AS secname');
        $query->select("CONCAT(ss.id,':',ss.alias) AS slug");
        $query->join('LEFT', '`#__cproject_sections` AS ss ON ss.id = a.section_id');

        // Join over the user
        $query->select('uc.name AS created_by');
        $query->join('LEFT', '`#__users` AS uc ON uc.id = a.created_by');


        // Join over the user
        $query->select('um.name AS modified_by');
        $query->join('LEFT', '`#__users` AS um ON um.id = a.modified_by');


        // add "where" into sql statement
        if (count($where)) {
            foreach ($where as $s) {
                $query->where($s);
            }
        }

        // add "order" into sql statement
        if (count($order)) {
            foreach ($order as $s) {
                $query->order($s);
            }
        } else {
            $query->order('a.id');
        }

        // add "group" into sql statement
        if (count($group)) {
            foreach ($group as $s) {
                $query->group($s);
            }
        }

        if ($sql) {
            return $query;
        } else {
            if ($limit > 0) {
                $db->setQuery($query, $limit);
            } else {
                $db->setQuery($query);
            }
            return $db->loadObjectList();
        }
    }
     function getCprojects($sql, $where = array(), $order = array(), $limit = 0, $group = array()) {

        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select("a.*");
        $query->select("CONCAT(a.id,':',a.alias) AS gslug");
        $query->from('#__cproject AS a');

        // Join over the sections
        $query->select('ss.name AS secname');
        $query->select("CONCAT(ss.id,':',ss.alias) AS slug");
        $query->join('LEFT', '`#__cproject_sections` AS ss ON ss.id = a.section_id');

         // Join over the categories
        $query->select('cc.name AS catname');
        $query->select("CONCAT(cc.id,':',cc.alias) AS cslug");
        $query->join('LEFT', '`#__cproject_categories` AS cc ON cc.id = a.category_id');
        
        // Join over the user
        $query->select('uc.name AS created_by');
        $query->join('LEFT', '`#__users` AS uc ON uc.id = a.created_by');


        // Join over the user
        $query->select('um.name AS modified_by');
        $query->join('LEFT', '`#__users` AS um ON um.id = a.modified_by');


        // add "where" into sql statement
        if (count($where)) {
            foreach ($where as $s) {
                $query->where($s);
            }
        }

        // add "order" into sql statement
        if (count($order)) {
            foreach ($order as $s) {
                $query->order($s);
            }
        } else {
            $query->order('a.id');
        }

        // add "group" into sql statement
        if (count($group)) {
            foreach ($group as $s) {
                $query->group($s);
            }
        }

        if ($sql) {
            return $query;
        } else {
            if ($limit > 0) {
                $db->setQuery($query, $limit);
            } else {
                $db->setQuery($query);
            }
            return $db->loadObjectList();
        }
    }

}

?>
